package com.company.project.modular.inStock.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.company.project.core.base.controller.BaseController;
import com.company.project.core.base.tips.ErrorTip;
import com.company.project.core.log.LogObjectHolder;
import com.company.project.modular.system.service.IInstockService;
import com.company.project.modular.system.service.IStandardService;
import com.company.project.modular.system.model.Instock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

/**
 * 库存查询控制器
 *
 * @author fengshuonan
 * @Date 2018-11-08 10:21:38
 */
@Controller
@RequestMapping("/instock")
public class InstockController extends BaseController {

    private String PREFIX = "/inStock/instock/";

    @Reference
    private IInstockService instockService;
    @Reference
    private IStandardService standardService;

    /**
     * 跳转到库存查询首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "instock.html";
    }

    /**
     * 跳转到添加库存查询
     */
    @RequestMapping("/instock_add")
    public String instockAdd() {
        return PREFIX + "instock_add.html";
    }

    /**
     * 跳转到添加入库
     */
    @RequestMapping("/instock_addIn")
    public String instockAddIn(Model model) {
        return PREFIX + "instock_addIn.html";
    }

    /**
     * 跳转到添加入库
     */
    @RequestMapping("/instock_addOut")
    public String instockAddOut(Model model) {
        return PREFIX + "instock_addOut.html";
    }

    /**
     * 获取标识码
     */
    @RequestMapping("/standardCode")
    @ResponseBody
    public List<String> standardCode(Model model) {
        /*查询标准物质表所有的的标识码*/
        List<String> standardCode = standardService.findByStandarCode();
        return standardCode;
    }

    /**
     * 跳转到修改库存查询
     */
    @RequestMapping("/instock_update/{instockId}")
    public String instockUpdate(@PathVariable Integer instockId, Model model) {
        Instock instock = instockService.selectById(instockId);
        model.addAttribute("item",instock);
        LogObjectHolder.me().set(instock);
        return PREFIX + "instock_edit.html";
    }

    /**
     * 获取库存查询列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition,Model model) {
        List<Map<String, Object>> instock = this.instockService.list(condition);
        return instock;
        /*if (condition == null) {
            return instockService.selectList(null);
        } else {
           if ("0".equals(condition)) {
                List<Map<String, Object>> instockNumber0 = this.instockService.instockNumber0(condition);
                return instockNumber0;
            }else{
                List<Map<String, Object>> instock = this.instockService.list(condition);
                return instock;
            }
        }*/
    }

    /**
     * 新增库存查询
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Instock instock,String standardCode, BindingResult bindingResult) {
       /* 判断a表的某字段（code）是否在b表code里面，还可以把code传入查询，select count(1) from a  where a.standardCode=b.standardCode;结果大于1则插入，否则不插入*/
        /*查询standard*/
       /* List<Standard> standard= standardService.findByStandar();
        List<String> standardCodeList = new ArrayList<>();
        for(int i = 0; i < standard.size(); i++){
            String standardCode = standard.get(i).getStandardCode();
            standardCodeList.add(standardCode);
        }*/
        /*库存表里面的标识码是否存在于标准物质表的标识码*/
        /*查询标识码*/
        List<String> standardCodeList = standardService.findByStandarCode();
        String sc = instock.getStandardCode();
        if (standardCodeList.contains(sc) && !instock.getStandardCode().equals(null)) {
            /*入库次数*/
            List<String> standardInFrequencyList = instockService.findByInFrequency(sc);
            String standardInFrequencyS = standardInFrequencyList.get(0);
            int standardInFrequency = Integer.valueOf(standardInFrequencyS);
            if("null".equals(standardInFrequencyS)){
                int inf = 1;
                instock.setStandardInFrequency(inf);
            }else {
                int inf = standardInFrequency + 1;
                instock.setStandardInFrequency(inf);
            }
            /*出库次数*/
            List<String> standardOutFrequency = instockService.findByOutFrequency(standardCode);
            if("null".equals(standardOutFrequency)){
                int outf = 1;
                instock.setStandardOutFrequency(outf);
            }else {
                int standardOutFrequencyInt = Integer.valueOf(standardOutFrequency.get(0));
                int outf = standardOutFrequencyInt + 1;
                instock.setStandardOutFrequency(outf);
            }
           /* 剩余量*/
            Double standardNumber = instock.getStandardInNumber()-instock.getStandardOutNumber();
            instock.setStandardNumber(standardNumber);

            instockService.insert(instock);
        }else{
            return new ErrorTip(0, "标识码不存在,请参考标准物质表的标识码");
        }
        return new ErrorTip(200, "添加成功");
    }

    /**
     * 新增入库
     */
    @RequestMapping(value = "/addIn")
    @ResponseBody
    public Object addIn(Instock instock, String standardCode,Double standardInNumber,BindingResult bindingResult) {
        /*Instock instockAll = instockService.findByAll();*/
        /*库存表里面的标识码是否存在于标准物质表的标识码*/
        /*查询所有的标识码*/
        List<String> standardCodeList = standardService.findByStandarCode();
        if(standardCode==null || standardCode.isEmpty()){
            return new ErrorTip(10, "标识码不能为空");
        }else{
            if (standardCodeList.contains(standardCode)) {
                /*入库次数*/
                List<String> standardInFrequencyList = instockService.findByInFrequency(standardCode);
                String standardInFrequencyS = standardInFrequencyList.get(0);
                /*出入库总次数*/
                List<String> standardFrequencyList = instockService.findByFrequency(standardCode);
                String standardFrequencyS = standardFrequencyList.get(0);

                if(standardInFrequencyS==null||standardInFrequencyS.isEmpty()){
                    int inf = 1;
                    instock.setStandardInFrequency(inf);
                    /* 初始出入库数量为空*/
                    instock.setStandardFrequency(inf);
                    /* 初始剩余量为空*/
                    instock.setStandardNumber(standardInNumber);
                }else {
                    /*入库次数*/
                    int standardInFrequency = Integer.valueOf(standardInFrequencyS);
                    int inf = standardInFrequency + 1;
                    instock.setStandardInFrequency(inf);
                    /*出入库总次数*/
                    int standardFrequency = Integer.valueOf(standardFrequencyS);
                    int f = standardFrequency + 1;
                    instock.setStandardFrequency(f);
                    /* 根据出入库总次数数来查询相应的剩余量*/
                    List<String> standardNumberList = instockService.findByNumber(standardFrequency);
                    double standardNumberList1 = Double.valueOf(standardNumberList.get(0));
                    double standardNumberList2 = standardNumberList1 + standardInNumber;
                    instock.setStandardNumber(standardNumberList2);
                }

                instockService.insert(instock);
            }else{
                return new ErrorTip(0, "标识码不存在,请参考标准物质表的标识码");
            }
            return new ErrorTip(200, "添加成功");
        }
    }
    /**
     * 新增出库
     */
    @RequestMapping(value = "/addOut")
    @ResponseBody
    public Object addOut(Instock instock, String standardCode,Double standardOutNumber,BindingResult bindingResult) {
        List<String> standardCodeList = standardService.findByStandarCode();
        if (standardCodeList.contains(standardCode) && !instock.getStandardCode().equals(null)) {
            /*出库次数*/
            List<String> standardOutFrequency = instockService.findByOutFrequency(standardCode);
            String standardOutFrequencyS = standardOutFrequency.get(0);
            /*出入库总次数*/
            List<String> standardFrequencyList = instockService.findByFrequency(standardCode);
            String standardFrequencyS = standardFrequencyList.get(0);

            if(standardOutFrequencyS == null || standardOutFrequencyS.isEmpty()){
                int outf = 1;
                instock.setStandardOutFrequency(outf);
            }else {
                /*保存出库次数*/
                int outfInt = Integer.valueOf(standardOutFrequency.get(0));
                int outf = outfInt + 1;
                instock.setStandardOutFrequency(outf);
            }
            /*出入库总次数*/
            int standardFrequency = Integer.valueOf(standardFrequencyS);
            int f = standardFrequency + 1;
            instock.setStandardFrequency(f);
            /* 根据出入库总次数数来查询相应的剩余量*/
            List<String> standardNumberList = instockService.findByNumber(standardFrequency);
            double standardNumberList1 = Double.valueOf(standardNumberList.get(0));
            double standardNumberList2 = standardNumberList1 - standardOutNumber;
            if(standardNumberList2<standardOutNumber){
                return new ErrorTip(0, "出库量不能大于库存量，剩余量为:"+ standardNumberList1);
            }
            instock.setStandardNumber(standardNumberList2);
            instockService.insert(instock);
        }else{
            return new ErrorTip(0, "标识码不存在,请参考标准物质表的标识码");
        }
        return new ErrorTip(200, "添加成功");
    }
    /**
     * 删除库存查询
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer instockId) {
        instockService.deleteById(instockId);
        return SUCCESS_TIP;
    }

    /**
     * 修改库存查询
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Instock instock) {
        instockService.updateById(instock);
        return SUCCESS_TIP;
    }

    /**
     * 库存查询详情
     */
    @RequestMapping(value = "/detail/{instockId}")
    @ResponseBody
    public Object detail(@PathVariable("instockId") Integer instockId) {
        return instockService.selectById(instockId);
    }
}
